package pictionary2;


import java.awt.Color;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.net.Socket;

public class Escuchador implements Runnable{
    private Socket s;
    private MSN msn;

    public Escuchador(Socket s, MSN m) {
        this.s = s;
        msn=m;
        Thread t= new Thread(this);
    }

    public void escucha(){
        try{
        Object str;
        //BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        InputStream is= s.getInputStream();
        
        System.out.println("Conexion aceptada de "+ s.getInetAddress().getCanonicalHostName());
        do {
            ObjectInputStream ois= new ObjectInputStream(is);
            //System.out.println("Se esta escucnahdo--"+str);
            str=ois.readObject();
            if(str instanceof String){
                //System.out.println("se leyo chidooooo--"+str);
                msn.getContactoActual().getOtroMensaje().append("\nAlguien >>"+(String)str);
             }else /*if(str instanceof Point){
                 msn.getPaint().setBackground(Color.red);
                 System.out.println(str);
                 msn.getPaint().setPoints((Point[])str);                 
             }else if(str instanceof Integer){
                 msn.getPaint().setBackground(Color.blue);
                 System.out.println("integer");
                 msn.getPaint().setPointCount(Integer.parseInt((String)str));                 
             }else if(str instanceof PaintPanel){
                 System.out.println("llego un paint panel");
                 msn.setPaint((PaintPanel)str);
             }*/{}
            //ois.close();
        }while(!str.equals("alto"));
        //ois.close();
        s.close();
        } catch(Exception e){
            System.out.println("Execption escuchador en escucha: "+e.getMessage());
            //escucha();
        }
    }



    /******public void escucha(){
        try{
        String str="";
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        System.out.println("Conexion aceptada de "+ s.getInetAddress().getCanonicalHostName());
        do {
            //System.out.println("Se esta escucnahdo");
            str=br.readLine();
            msn.getContactoActual().getOtroMensaje().append("\nAlguien >>"+str);
        }while(!str.equals("alto"));
        br.close();
        s.close();
        }catch(Exception e){
            System.out.println("Execption escuchador en escucha");
        }
    }*/



    public void run() {
        escucha();
    }

}
